package com.example.springboot.controller;

import com.example.springboot.common.tools.Layui;
import com.example.springboot.common.vo.LogoInfo;
import com.example.springboot.common.vo.Menu;
import com.example.springboot.common.vo.PermissionsMenu;
import com.example.springboot.common.vo.TreeMenu;
import com.example.springboot.entity.SysMenu;
import com.example.springboot.service.SysMenuService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * (SysMenu)表控制层
 *
 * @author makejava
 * @since 2021-09-17 19:54:38
 */
@RestController
@RequestMapping("URMS/sysMenu")
public class SysMenuController {
	/**
	 * 服务对象
	 */
	@Resource
	private SysMenuService sysMenuService;

	/**
	 * 通过主键查询单条数据
	 *
	 * @param id 主键
	 * @return 单条数据
	 */
	@RequestMapping(value = "selectOne", method = RequestMethod.GET)
	public SysMenu selectOne(Long id) {
		return this.sysMenuService.queryById(id);
	}

	/**
	 * 通过主键查询单条数据
	 *
	 * @param id 主键
	 * @return 单条数据
	 */
	@RequestMapping(value = "queryUserMenuById", method = RequestMethod.GET)
	public Menu queryUserMenuById(Long id) {
		PermissionsMenu treeMenu = new PermissionsMenu();
		treeMenu.setTitle("常规管理");
		treeMenu.setHref("");
		treeMenu.setIcon("fa fa-address-book");
		treeMenu.setTarget("_self");
		treeMenu.child = this.sysMenuService.queryUserMenuById(id);

		Menu menu = new Menu();
		menu.homeInfo = new LogoInfo();
		menu.homeInfo.title = "首页";
		menu.homeInfo.href = "";
		menu.logoInfo = new LogoInfo();
		menu.logoInfo.title = "LAYUI MINI";
		menu.logoInfo.href = "";
		menu.logoInfo.image = "images/logo.png";
		menu.menuInfo = new ArrayList<>();
		menu.menuInfo.add(treeMenu);
		return menu;
	}


	/**
	 * 查询全部数据
	 *
	 * @return 所有菜单数据
	 */
	@RequestMapping(value = "selectAllMenu", method = RequestMethod.GET)
	public Layui selectAllMenu(HttpSession session) {

		List<SysMenu> sysMenuList = this.sysMenuService.queryAll(null);
		session.setAttribute("sysMenuList", sysMenuList);
		return Layui.data(sysMenuList.size(), sysMenuList);
	}

	/**
	 * 查询全部数据
	 *
	 * @return 所有菜单数据
	 */
	@RequestMapping(value = "treeMenu", method = RequestMethod.POST)
	public List<TreeMenu> treeMenu() {

		List<TreeMenu> treeMenuList = this.sysMenuService.queryTreeMenu(0L);
		return treeMenuList;
	}

	/**
	 * 查询全部数据
	 *
	 * @return 所有菜单数据
	 */
	@RequestMapping(value = "addSubmitMenu", method = RequestMethod.POST)
	public String addSubmitMenu(SysMenu sysMenu) {
		return this.sysMenuService.insert(sysMenu) != null ? "添加成功" : "添加失败";
	}

	/**
	 * 查询全部数据
	 *
	 * @return 所有菜单数据
	 */
	@RequestMapping(value = "modifySubmit", method = RequestMethod.POST)
	public boolean modifySubmit(SysMenu sysMenu) {

		System.out.println(sysMenu.toString());
		return this.sysMenuService.update(sysMenu) != null;
	}


}